﻿Imports System.Data.SqlClient

Public Class clsUsuarios
    Private mintIdUsuario As Integer
    Private mstrNombre As String
    Private mstrNombreCompleto As String
    Private mstrPassword As String

    Public Property IdUsuario() As Integer
        Get
            Return mintIdUsuario
        End Get
        Set(ByVal value As Integer)
            mintIdUsuario = value
        End Set
    End Property
    Public Property Nombre As String
        Get
            Return mstrNombre
        End Get
        Set(ByVal value As String)
            mstrNombre = value
        End Set
    End Property
    Public Property NombreCompleto As String
        Get
            Return mstrNombreCompleto
        End Get
        Set(ByVal value As String)
            mstrNombreCompleto = value
        End Set
    End Property
    Public Property PassWord As String
        Get
            Return mstrPassword
        End Get
        Set(ByVal value As String)
            mstrPassword = value
        End Set
    End Property
    Public Sub New()
        mintIdUsuario = 0
        mstrNombre = ""
        mstrNombreCompleto = ""
        mstrPassword = ""
    End Sub
    Public Sub Borrar()

        Call SetSqlInicial("Delete From Usuarios Where IdUsuario = " & mintIdUsuario)

    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Usuarios (" & _
                            "IdUsuario," & _
                            "Nombre," & _
                            "NombreCompleto," & _
                            "Password) " & _
                            "VALUES (" & _
                            "@IdUsuario," & _
                            "@Nombre," & _
                            "@NombreCompleto," & _
                            "@Password) "

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = InitialConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdUsuario", mintIdUsuario)
        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("NombreCompleto", mstrNombreCompleto)
        Command.Parameters.AddWithValue("Password", mstrPassword)
       
        cnConn.Open()
        Dim RowsAffected As Integer
        Try
            RowsAffected = Command.ExecuteNonQuery
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        End Try

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el Empleado " & mstrNombreCompleto, MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Sub Actualizar()
        Dim strSqlQry As String
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = InitialConnection

        strSqlQry = "Update Usuarios Set " & _
                            "NombreCompleto=@NombreCompleto," & _
                            "Password=@Password " & _
                            "Where IdUsuario=@IdUsuario"

        Dim Command As New SqlCommand(strSqlQry, cnConn)
        Command.Parameters.AddWithValue("IdUsuario", mintIdUsuario)
        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("NombreCompleto", mstrNombreCompleto)
        Command.Parameters.AddWithValue("Password", mstrPassword)

        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el registro", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If



    End Sub
End Class
